package com.remon.quartz.job;

import com.remon.quartz.util.SpringUtils;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

public class JobHttpRequest implements BaseJob {

    private static Logger _log = LoggerFactory.getLogger(JobHttpRequest.class);

    public JobHttpRequest() {

    }

    public void execute(JobExecutionContext context)
            throws JobExecutionException {

        RestTemplate restTemplate = SpringUtils.context.getBean(RestTemplate.class);

        JobDetail jobDetail = context.getJobDetail();
        final String reqUrl = jobDetail.getJobDataMap().getString("reqUrl");
        final String jobData = jobDetail.getJobDataMap().getString("payload");
        final String reqMethod = jobDetail.getJobDataMap().getString("reqMethod");

        if (reqMethod.toUpperCase().equals(RequestMethod.GET.name())) {
            final String json = restTemplate.getForObject(reqUrl, String.class);
            _log.error("return json @" + json);
        } else if (reqMethod.toUpperCase().equals(RequestMethod.POST.name())) {
            final String json = restTemplate.postForObject(reqUrl, jobData, String.class);
            _log.error("return json @" + json);
        }

    }
} 